<?php
/**
 * @园区类
 * 1、园区列表
 * 2、园区详情
 * 
 */
namespace app\park\controller;
use app\common\controller\HomeCommon;
use think\Cache;
use think\Config;

class Index extends HomeCommon
{
	public function __construct(){
		
		parent::__construct();
		
	
	}
	
	
    /**
     * 获得园区列表
    */
    public function get_park(){


    	$where="  a.status=1  ";

    	$field="park_name,identification,thumb,city,prime_mover_industry,park_introduction,level,park_area,land_price,address";
    	$obj=db('parks a');


   /***关键字*/
    	$keywords=request()->param('keywords','','trim');
    	if(!empty($keywords)){
    		$where.=" and a.park_name like '%$keywords%' ";
    	}
   /***行业*/
    	$industry_id=request()->param('industry_id',0,'intval');
    	if(!empty($industry_id)){
    		$obj=$obj->join('parks_industry b','a.id=b.park_id');
    		$where.=" and b.status=1 and b.industry_id=$industry_id";
    	}
   /***产业链*/
    	$chain_id=request()->param('chain_id',0,'intval');
    	if(!empty($chain_id)){
    		$obj=$obj->join('parks_chain c','a.id=c.park_id','left');
    		$where.=" and c.status=1 and c.chain_id=$chain_id";
    	}
   /***城市*/
    	$city_id=request()->param('city_id',0,'intval');
    	if(!empty($city_id)){
    		$where.=" and (a.province_id=$city_id or a.city_id=$city_id or a.area_id=$city_id ) ";
    	}
        /**园区类型**/
        $level=request()->param('level','','trim');
        if (!empty($level)){
            switch ($level) {
                //国家级
                case 'state':
                    $obj->where('a.level', '国家级');
                    break;
                //省级
                case 'province':
                    $obj->where('a.level', '省级');
                    break;
                //市区级
                case 'area':
                    $obj->where('a.level', '地市/县级');
                    break;
                //经开区
                case 'jk':
                    $obj->where('a.level', '经开区');
                    break;
                //高新区
                case 'gx':
                    $obj->where('a.level', '高新区');
                    break;
                //产业园
                case 'cy':
                    $obj->where('a.level', '产业园');
                    break;
                //加速器
                case 'js':
                    $obj->where('a.level', '加速器');
                    break;
                //孵化器
                case 'fh':
                    $obj->where('a.level', '孵化器');
                    break;
                //共享空间
                case 'gxkj':
                    $obj->where('a.level', '共享空间');
                    break;
                default:
                    exit('未知排序字段1');
                    break;
            }
        }
        /**排序字段**/
        $sortBy = request()->param('sortBy', 'zhpg', 'trim');
        if (!empty($sortBy)){
            switch ($sortBy) {
                //综合评估
                case 'zhpg':
                    $obj->order('a.score', 'desc');
                    break;
                //产业聚集
                case 'cyjj':
                    $obj->order('a.score', 'desc');
                    break;
                //产业环境
                case 'cyhj':
                    $obj->order('a.score', 'desc');
                    break;
                //经济基础
                case 'jjjc':
                    $obj->order('a.score', 'desc');
                    break;
                //人才基础
                case 'rcjc':
                    $obj->order('a.score', 'desc');
                    break;
                //政策支持
                case 'zczc':
                    $obj->order('a.score', 'desc');
                    break;
                default:
                    exit('未知排序字段2');
                    break;
            }
        }

        // 设置分页数据
        $page=request()->param('page',1,'intval');
        $limit=request()->param('limit',15,'intval');
        Config::set('paginate.page',$page);
        $listobj=$obj->field($field)->where($where)->order('a.id','asc')->paginate($limit);


        return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$listobj]);
    }
  /**
     * 获得园区详情
   *
    */
    public function get_park_info(){
    	$where['status']=['=',1];
    	
    	
    	$identification=request()->param('identification','','trim');
    	
    	
    	if(!empty($identification)){
    		$where['identification']=['=', $identification];
    		
    		
    		$cache=new Cache();
    		$result=$cache->get('parks_'.$identification);
    		
    		
    		if($result&&1==2){
    			$info=unserialize($result);
    		}else{
    			$field="id,park_name,thumb,contact,prime_mover_industry,tel,city_str as city,address,level,affair_url,park_area,lat,lng,identification,land_price,province,p_city,score";
    			$model= model('Commondata');
    			$info=$model->get_detail('parks',$field,$where);

              //招商信息
                $basic=db('parks_basic')->where(['park_id'=>$info['id']])->find();
              //发展规划
                $develop=db('parks_develop')->where(['park_id'=>$info['id']])->find();
              //地区政策
                $local_policy=db('parks_local_policy a')->join('policy b','a.policy_id=b.id')
                             ->where(['a.park_id'=>$info['id']])
                             ->order('a.id desc')
                             ->select();
              //投资成本
                $invest_cost=db('parks_invest_cost')->where(['park_id'=>$info['id']])->find();
              //优惠政策
                $favoured_policy=db('parks_favoured_policy a')->join('policy b','a.policy_id=b.id')
                             ->where(['a.park_id'=>$info['id']])
                             ->order('a.id desc')
                             ->select();
              //园区资源
                $resource=db('parks_resource')->where(['park_id'=>$info['id']])->find();
                             
              //环境配套
                $match=db('parks_match')->where(['park_id'=>$info['id']])->find();

              //园区新闻
                $news=db('parks_news')->where(['park_id'=>$info['id']])->find();

                $info['city']= unserialize($info['city']);
                unset($info['id']);
                $info['basic']=$basic;
                $info['develop']=$develop;
                $info['local_policy']=$local_policy;
                $info['invest_cost']=$invest_cost;
                $info['favoured_policy']=$favoured_policy;
                $info['resource']=$resource;
                $info['match']=$match;
                $info['news']=$news;
               


    			$cache->set('parks_'.$identification, serialize($info),3600);
    		}
    		
    		
    		return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$info]);
    		
    		
    	}else{
    		return json(['errcode'=>'-1','msg'=>lang('param error'),'data'=>[]]);
    	}
    	
    	
    	
    	
    	
    }
}